package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.venda.ItemVenda;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = ItemVenda.class)
public class ItemVendaConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringItemVenda) {

		if (stringItemVenda == null)
			return null;

		if (stringItemVenda.trim().length() == 0)
			return null;

		Long idItemVenda = Long.valueOf(stringItemVenda);
		return this.serviceFactory.getVendaService().consultarItemVenda(
				idItemVenda);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objItemVenda) {
		if (objItemVenda == null)
			return null;
		ItemVenda itemVenda = (ItemVenda) objItemVenda;
		return String.valueOf(itemVenda.getIdItemVenda());
	}
}
